//
//  LHSkuCell.m
//  lehuotest
//
//  Created by ice on 2017/1/5.
//  Copyright © 2017年 QF. All rights reserved.
//

#import "LHSkuCell.h"
#import "HomeGetAttrModel.h"
#import <UIView+MJExtension.h>
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define LineWidth 25


@interface LHSkuCell()

@property (nonatomic, strong) NSMutableArray *btnArray;

@end

@implementation LHSkuCell


-(NSMutableArray *)btnArray
{
    if (!_btnArray){
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+(instancetype) initWithTableView:(UITableView *)tableView
{
    static NSString *LHSkuCellIdentifier = @"LHSkuCellIdentifier";
    LHSkuCell *cell = [tableView dequeueReusableCellWithIdentifier:LHSkuCellIdentifier];
    if (cell == nil){
        cell = [[LHSkuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:LHSkuCellIdentifier];
    }
    
    return cell;
}
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
    }
    return self;
}
-(void) setSkus:(NSArray *)skus
{
    _skus = skus;
    if (skus.count == 0){
        return;
    }
    CGFloat w = 0;
    CGFloat h = 0;
    for (int i = 0 ; i < skus.count ; i++) {
        HomeAttValuesModel *model = skus[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btnArray addObject:btn];
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 3.f;
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        btn.layer.borderWidth = .5f;
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGFloat length = [model.label boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        [btn setTitle:model.label forState:UIControlStateNormal];
        //设置button的frame
        btn.frame = CGRectMake(10 + w, h, length + 15 , 30);
        //当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度
        if(10 + w + length + 15 > SCREEN_WIDTH){
            w = 0; //换行时将w置为0
            h = h + btn.frame.size.height + 10;//距离父视图也变化
            btn.frame = CGRectMake(10 + w, h, length + 15, 30);//重设button的frame
        }
        w = btn.frame.size.width + btn.frame.origin.x;
        [self.contentView addSubview:btn];
        if (i == skus.count -1){
            _cellHeight = btn.frame.origin.y+btn.frame.size.height;
        }
    }
    
    
}

-(void) handleClick:(UIButton *)sender
{
    [self resetAllBtn];
    [sender setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    sender.layer.borderColor = [UIColor orangeColor].CGColor;
}

-(void) resetAllBtn
{
    for (UIButton *btn in self.btnArray) {
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 3.f;
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        btn.layer.borderWidth = .5f;

    }
}


-(void) layoutSubviews
{
    [super layoutSubviews];
    
}

+(CGFloat) CellDefaultHeight:(NSArray *)skus
{
    if (!skus || skus.count == 0){
        return 0;
    }
    CGFloat w = 0;
    CGFloat h = 0;
    CGFloat result = 0.0;
    for (int i = 0 ; i < skus.count ; i++) {
        HomeAttValuesModel *model = skus[i];
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGFloat length = [model.label boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        //设置button的frame
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10 + w, h, length + 15 , 30);
        //当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度
        if(10 + w + length + 15 > SCREEN_WIDTH){
            w = 0; //换行时将w置为0
            h = h + btn.frame.size.height + 10;//距离父视图也变化
            btn.frame = CGRectMake(10 + w, h, length + 15, 30);//重设button的frame
        }
        w = btn.frame.size.width + btn.frame.origin.x;
        if (i == skus.count - 1){
            result = btn.frame.size.height+btn.frame.origin.y;
        }
    }
    return result+10;
}

@end
